package com.example.armmvvm.ui.constraint

import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.arm.ext.dpInt
import com.example.armmvvm.R
import com.example.armmvvm.databinding.LayoutFunctionBottomFallViewBinding
import com.example.armmvvm.ui.fallview.AnchorFallView
import com.example.armmvvm.ui.fallview.AnchorFallViewSetting
import com.example.armmvvm.ui.fallview.FallViewMode

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.constraint
 * @createTime 创建时间 ：2022/2/23
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class BottomFunctionFallView(lifecycleOwner: LifecycleOwner, viewContainer: ViewGroup) :
    AnchorFallView(lifecycleOwner, viewContainer, R.layout.layout_function_bottom_fall_view) {

    val adapter = BottomFunctionAdapter()

    lateinit var binding: LayoutFunctionBottomFallViewBinding

    private var functionMaxHeight: Int = -1

    private var functionMinHeight: Int = -1

    override fun updateView(view: View) {
        binding = LayoutFunctionBottomFallViewBinding.bind(view).apply {
            for (i in 0..14) {
                adapter.addData("22")
            }
            recyclerViewFunction.adapter = adapter
            (recyclerViewFunction.layoutManager as? GridLayoutManager)?.isSmoothScrollbarEnabled = false
        }
    }

    override fun bottomFallViewSetting(view: View): AnchorFallViewSetting? {
        return AnchorFallViewSetting(view).apply {
            mode = FallViewMode.TWO_NO_TOP_STATE
            calcHeight = {
                if (functionMaxHeight <= 0 && binding.recyclerViewFunction.childCount > 0) {
                    functionMaxHeight = binding.recyclerViewFunction.height
                    functionMinHeight = binding.recyclerViewFunction.getChildAt(0).bottom + 4.dpInt
                    binding.root.layoutParams = binding.root.layoutParams.apply {
                        height = binding.root.height
                    }
                }
                peekHeight = 100.dpInt
                anchorHeight = 100.dpInt
            }
            behaviorListener = {
                onStateChanged { _, _, newState ->

                }
                onSlide { bottomSheet, _ ->
                    val top = (bottomSheet.parent as View).height - bottomSheet.top
                    val peekHeight = controller.behavior.peekHeight
                    var scrollPercent = (top.toFloat() - peekHeight) / (bottomSheet.height - peekHeight)
                    scrollPercent = maxOf(scrollPercent, 0f)
                    scrollPercent = minOf(scrollPercent, 1f)
                    binding.recyclerViewFunction.layoutParams = binding.recyclerViewFunction.layoutParams.apply {
                        height = (functionMinHeight + (functionMaxHeight - functionMinHeight) * scrollPercent).toInt()
                    }
                }
            }
        }
    }

    class BottomFunctionAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_function_bottom_fall_view) {
        override fun convert(holder: BaseViewHolder, item: String) {

        }
    }
}